home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-04-05 | 6.0 KB | 264 lines | [TEXT/MMCC] |
- // ===========================================================================
- // SCDoc.cp
- // ===========================================================================
- // © 1995 James Kaput, Jeremy Roschelle SimCalc Project
-
- #include "SCDoc.h"
- #include "SCPage.h"
-
- // consts for appleEvents
- const DescType ae_Sort = 10000;
- const DescType keySortDirection = 'srtD';
- const DescType kSortAscending = '> ';
-
- SCDoc::SCDoc(LCommander *inSuper)
- : LDocument (inSuper), mSelection (nil)
- {
- // add five pages, just for fooling around with
- for (short i = 0; i < 5; ++i) new SCPage(this);
-
- // make us the default model
- // you might not choose to do this in your app
- // or you might want the frontmost doc to be the default model
- SetDefaultModel(this);
- // note that because we do this, we have to override MakeSelfSpecifier
- // so that we are the "null" (outermost) container
- }
-
- SCDoc::~SCDoc()
- {
- LListIterator iter(mPageList,iterate_FromEnd);
- SCPage *page;
- while(iter.Next(&page)) delete page;
- }
-
- StringPtr
- SCDoc::GetDescriptor(Str255 outDescriptor) const
- {
- CopyPStr("\puntitled",outDescriptor);
- return outDescriptor;
- }
-
- void
- SCDoc::SetSelection(SCPage *inPage)
- {
- mSelection = inPage;
- }
-
- void
- SCDoc::DoSort(Boolean inAscending)
- {
- LList list;
- LListIterator iter(mPageList, iterate_FromStart);
- SCPage *page, *comparePage;
- Str255 name, compareName;
- Int32 i, count = 0, index;
-
- // do insertion sort
- while(iter.Next(&page)) {
- page->GetDescriptor(name);
- index = count + 1;
- for(i = 1; i <= count; ++i) {
- list.FetchItemAt(i,&comparePage);
- comparePage->GetDescriptor(compareName);
- if (-1 == RelString(name,compareName,false,false)) {
- index = i;
- break;
- }
- }
- list.InsertItemsAt(1,index,&page);
- ++count;
- }
-
- // copy back, reversing order if need be
- mPageList.RemoveItemsAt(count,1);
- LListIterator iter2(list,iterate_FromStart );
- while(iter2.Next(&page))
- mPageList.InsertItemsAt(1,inAscending ? arrayIndex_Last : arrayIndex_First,&page);
- }
-
-
- void
- SCDoc::AddSubModel(LModelObject *inSubModel)
- {
- if (SCPage::modelKind == inSubModel->GetModelKind()) {
- mPageList.InsertItemsAt(1,arrayIndex_Last,&inSubModel);
- }
- }
-
- void
- SCDoc::RemoveSubModel(LModelObject *inSubModel)
- {
- if (SCPage::modelKind == inSubModel->GetModelKind()) {
- mPageList.Remove(inSubModel);
- }
- }
-
- Int32
- SCDoc::CountSubModels(DescType inModelID) const
- {
- if (inModelID == SCPage::modelKind) return (mPageList.GetCount());
- ThrowOSErr_(errAENotAnElement);
- return 0; // prevent compiler warning
- }
-
- Int32
- SCDoc::GetPositionOfSubModel(DescType inModelID,
- const LModelObject *inSubModel) const
- {
- if (inModelID == SCPage::modelKind) {
- return mPageList.FetchIndexOf(&inSubModel);
- }
-
- ThrowOSErr_(errAENotAnElement);
- return 0; // prevent compiler warning
- }
-
-
- void
- SCDoc::GetSubModelByPosition(
- DescType inModelID,
- Int32 inPosition,
- AEDesc &outToken) const
- {
- if (inModelID == SCPage::modelKind){
- SCPage *page;
-
- // LList::FetchItemAt not declared const (even though we know it is).
- // so we need this assignment and cast to sneak it by the compiler.
- LList *list = (LList *)&mPageList;
- if (list->FetchItemAt(inPosition,&page)) {
- PutInToken(page,outToken);
- return;
- }
- }
- ThrowOSErr_(errAENotAnElement);
- }
-
-
- void
- SCDoc::GetSubModelByName(DescType inModelID,
- Str255 inName,
- AEDesc &outToken) const
- {
- if (inModelID == SCPage::modelKind){
- SCPage *page;
- Str255 pageName;
- LListIterator iter(mPageList,iterate_FromStart);
-
- while (iter.Next(&page)) {
- page->GetDescriptor(pageName);
- if (EqualString(pageName,inName,false,false)) {
- PutInToken(page,outToken);
- return;
- }
- }
- }
- ThrowOSErr_(errAENotAnElement);
- }
-
-
- void
- SCDoc::GetSubModelByUniqueID(
- DescType inModelID,
- const AEDesc &inKeyData,
- AEDesc &outToken) const
- {
- long id,pageID;
-
- UExtractFromAEDesc::TheInt32(inKeyData,id);
-
- if (inModelID == SCPage::modelKind){
- SCPage *page;
- LListIterator iter(mPageList,iterate_FromStart);
-
- while (iter.Next(&page)) {
- pageID = page->GetID();
- if (pageID == id) {
- PutInToken(page,outToken);
- return;
- }
- }
- }
- ThrowOSErr_(errAENotAnElement);
- }
-
- void
- SCDoc::GetAEProperty(
- DescType inProperty,
- const AEDesc &inRequestedType,
- AEDesc &outPropertyDesc) const
- {
- switch (inProperty) {
- case 'psel': // selection property
- if (mSelection) mSelection->MakeSpecifier(outPropertyDesc);
- break;
- default:
- inherited::GetAEProperty(inProperty,inRequestedType,outPropertyDesc);
- }
-
- }
-
-
- void
- SCDoc::HandleAppleEvent(
- const AppleEvent &inAppleEvent,
- AppleEvent &outAEReply,
- AEDesc &outResult,
- Int32 inAENumber)
- {
- switch (inAENumber) {
- case ae_CountElements:
- HandleCountEvent(inAppleEvent, outAEReply, outResult);
- break;
- case ae_Sort:
- {
- StAEDescriptor desc;
- OSType direction = kSortAscending;
- desc.GetOptionalParamDesc(inAppleEvent,keySortDirection,typeEnumeration);
- if (desc.mDesc.dataHandle != nil) { // check for optional parameter
- UExtractFromAEDesc::TheEnum(desc.mDesc,direction);
- }
- DoSort(direction == kSortAscending);
- break;
- }
- default:
- inherited::HandleAppleEvent(inAppleEvent,
- outAEReply,
- outResult,
- inAENumber);
- break;
- }
- }
-
- void
- SCDoc::HandleCountEvent(const AppleEvent &inAppleEvent,
- AppleEvent &outAEReply,
- AEDesc &outResult)
- {
- OSErr err;
- DescType descType, objectType;
- long actualSize, count;
-
- err = AEGetParamPtr(&inAppleEvent, keyAEObjectClass,
- typeType,&descType,
- &objectType,
- sizeof(descType),&actualSize); /* IM VI chap. 6 pg 75 */
- FailOSErr_(err);
-
- count = CountSubModels(objectType);
- err = AECreateDesc(typeLongInteger, (Ptr) &count, sizeof(long), &outResult);
- FailOSErr_(err);
- }
-
- // we need to override this to return the null container because we have set ourself to be the
- // default container
- void
- SCDoc::MakeSelfSpecifier(
- AEDesc &inSuperSpecifier,
- AEDesc &outSelfSpecifier) const
- {
- outSelfSpecifier.descriptorType = typeNull;
- outSelfSpecifier.dataHandle = nil;
- }
-